﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GalaxyPresident
{
    public class AirPort : OwnEntity,CraftStation
    {
        private static Texture2D texture = Constant.content.Load<Texture2D>(@"Resources/airPort");
        private static Texture2D texture_s = Constant.content.Load<Texture2D>(@"Resources/airPort_s");

        private List<Unit> PROTOTYPE_UNIT = new List<Unit>();
        //private List<Unit> units = new List<Unit>();

        public override string getObjectName()
        {
            return "Barrack";
        }

        public AirPort(Planet planet)
            : base(planet)
        {            
            sprite_s = new Sprite(texture_s, 1, 1,
                new List<int>{0},150);
            sprite = new Sprite(texture, 1, 4,
                new List<int> { 0, 1, 2, 3 }, 150);

            initSprites(planet);

            if (PlanetOwner == null)
                return;

            // TODO: load all Units.
            PROTOTYPE_UNIT.Add(new SpaceCraft(PlanetOwner));
            PROTOTYPE_UNIT.Add(new AirCraft(PlanetOwner));
            PROTOTYPE_UNIT.Add(new BomberCraft(PlanetOwner));
            PROTOTYPE_UNIT.Add(new MotherShip(PlanetOwner));

            

        }
        protected override void InitAvailableTasks()
        {
            base.InitAvailableTasks();
            for(int i=0;i<3;i++)
            {
                AvailableTasks.Add(new CreateUnitTask(this,i,
                    @"Resources/Ingame/bUnit"+i.ToString(),PlayingScreen.BUTTON_COLOR_1));
            }
            AvailableTasks.Add(new CreateMotherShipTask(this,
                    @"Resources/Ingame/bMotherShip", PlayingScreen.BUTTON_COLOR_3));
        }
        public void AddUnit(Unit unit)
        {
            if (unit == null)
            {
                return;
            }
            unit.Station = this;
            _planetOwner.AddUnit(unit);
            //_planetOwner.Units.Add(unit);
        }
        public Unit RemoveUnit(Unit unit)
        {
            _planetOwner.Units.Remove(unit);
            _planetOwner.RemoveUnit(unit);
            return unit;
        }
        public Planet GetPlanet()
        {
            return _planetOwner;
        }
        public Unit createUnit(int index)
        {
            if (index < 0 || index > PROTOTYPE_UNIT.Count)
                return null;
            PROTOTYPE_UNIT[index].Scale = Constant.sizeBlock / 200;
            return PROTOTYPE_UNIT[index].clone();
        }

        protected override void initInfo()
        {
            meterialCost = 1500;
            moneyCost = 2000;
            energyCost = 200;

            _healthPoint = 3000;
            _defense = 0.3f;
        }

        public bool isEnoughResourcesForProduce(int unitIndex)
        {
            return PROTOTYPE_UNIT[unitIndex].isEnoughResourcesForProduce(PlanetOwner);
        }
        public override string getInfoString()
        {
            return base.getInfoString();
        }

        public Unit getUnit(int unitIndex)
        {
            return PROTOTYPE_UNIT[unitIndex];
        }
        public bool IsAlert(Vector2 armyPosition)
        {
            if (armyPosition.Length()<_planetOwner.Diameter*Constant.scalePlanetEntity/2+200)
            {
                return true;
            }
            return false;
        }
        public void Attack(Vector2 position)
        {
            foreach (Unit unit in _planetOwner.Units)
            {
                if (!unit.IsOn)
                {
                    unit.IsOn = true;
                    _planetOwner.AddToLayer(unit);
                    unit.moveTo(position);
                    return;
                }
            }
        }
        public void Attack(PlanetObjectOwner obj)
        {
            foreach (Unit unit in _planetOwner.Units)
            {
                if (!unit.IsOn)
                {
                    unit.Position = position;
                    _planetOwner.AddToLayer(unit);
                    unit.attackTo(obj);
                    unit.IsOn = true;
                    return;
                }
            }
        }
        public Team GetTeamOwner()
        {
            return _planetOwner.TeamOwner;
        }
        public void Withdraw()
        {
            foreach (Unit unit in _planetOwner.Units)
            {
                if (unit.IsOn)
                {
                    if (unit.Position == position)
                    {
                        unit.Parent.RemoveChild(unit);
                        unit.IsOn = false;
                    }
                    else
                    {
                        //_planetOwner.AddToLayer(unit);
                        unit.moveTo(position);
                    }
                    //return;
                }
            }
        }
    }
}
